/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pl.cezary.butler.walili.client.db.tasks;

import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import pl.cezary.butler.walili.client.DownloadTask;
import pl.cezary.butler.walili.client.DownloadTaskStatus;
import pl.cezary.butler.walili.server.db.tasks.UpdateTask;

/**
 *
 * @author Czarek
 */
public class NewDownloadTask extends UpdateTask {

    private static final Logger log = Logger.getLogger(NewDownloadTask.class.getName());
    private final String task;

    public NewDownloadTask(String task) {
        this.task = task;
    }

    @Override
    public void execute() throws SQLException {
        final boolean result = getJdbcTemplate().queryForObject("select exists(Select id from filesToDownload where url=?)", Boolean.class, task);
        if (!result) {
            if (log.isLoggable(Level.FINE)) {
                log.log(Level.FINE, "New download task with URL: {0} was added", task);
            }
            getJdbcTemplate().update("INSERT INTO filesToDownload(url,status) values(?,?)", task, DownloadTaskStatus.NEW.ordinal());
        } else {
            log.log(Level.INFO, "Download task with URL: {0} was found and therefore it wasn't added", task);
        }
    }
}
